var roomManager = require('../../../services/bjl/roomManager');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

var handler = Handler.prototype;

handler.bid = function (msg, session, next) {
    console.log('gameHandler.bid caught >>>>>>>>>>>>>>>>>>>>');
    console.log(msg);
    var gameId = session.get('gameId');
    var roomId = session.get('roomId');
    var playerId = session.get('playerId');

    // 提纯参数(去掉参数中的route信息), 并将计算单位由元转化为分
    var count = 0;

    for (var k in msg) {
        if (!isNaN(msg[k])) {
            count++;
            msg[k] = (parseInt(msg[k]) * 100);
        }
        else {
            // 删掉非下注信息
            delete msg[k];
        }
        // 因为客户端现在不缓存下注，所以每次只会提交一个位置的下注信息
        // 如果每次的下注信息超过两个位置的，那么就有可能是黑客攻击
        if (count > 2) {
            return next(null, { ret: '下注信息有误！' });
        }
    }

    var ret = roomManager.bid(roomId, playerId, msg);

    next(null, { ret });
};

// 申请上庄
// msg = { playerId: 123, roomId: 1};
handler.applyBanker = function (msg, session, next) {
    console.log('gameHandler.applyBanker caught >>>>>>>>>>>>>>>>>>>>>>>>>');
    var playerId = session.get('playerId') || 0;
    var roomId = session.get('roomId') || 0;
    if (playerId != msg.playerId || roomId != msg.roomId) {
        next(null, { err: 'id can not match' });
        return;
    }
    roomManager.applyBanker(msg, function (err) {

        var ret = {};
        if (err) {
            console.log(err);
            ret = { err };
        }
        next(null, ret);
    });
};

// 申请下庄
// msg = { playerId: 123, roomId: 1};
handler.cancelBanker = function (msg, session, next) {
    console.log('gameHandler.cancelBanker caught >>>>>>>>>>>>>>>>>>>');
    var playerId = session.get('playerId');
    var roomId = session.get('roomId');
    if (playerId != msg.playerId || roomId != msg.roomId) {
        next(null, { err: 'id can not match' });
        return;
    }
    roomManager.cancelBanker(msg, function (data, result) {
        console.log(data);
        var ret = {};
        if (!result) {
            ret = data;
        }
        next(null, ret);
    });
};

// 申请插队上庄
handler.cutInBanker = function (msg, session, next) {
    console.log('gameHandler.cutInBanker caught >>>>>>>>>>>>>>>>>>>>>>>');

    var playerId = session.get('playerId');
    var roomId = session.get('roomId');
    if (playerId != msg.playerId || roomId != msg.roomId) {
        next('id can not match', {});
        return;
    }
    roomManager.cutInBanker(msg, function (rel) {
        console.log(rel);
        next(null, rel);
    });
};

handler.getRoomList = function (msg, session, next) {
    var ret = roomManager.getRoomList(msg);
    next(null, { ret });
};

handler.chatInRoom = function (msg, session, next) {
    var roomId = session.get('roomId');
    msg.roomId = roomId;
    var ret = roomManager.chatInRoom(msg);
    next(null, { ret });
};

handler.exiGame = function (msg, session, next) {
    var exi = msg.exi;
    console.log('停止游戏！');
    if (exi) {
        process.exit();
    }
    next(null, {msg: '停止成功！'});
};

